//
//  VC2Button.swift
//  ChapterAll
//
//  Created by Kevin on 2020/4/21.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VC2Button: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let btn = UIButton(type: .roundedRect)
        btn.backgroundColor = UIColor.red
        btn.setTitle("点我一下", for: .normal)
        btn.frame = CGRect(x: 10, y: 70, width: 200, height: 50)
        btn.addTarget(self, action: #selector(clickMe), for: .touchUpInside)
        btn.setBackgroundImage(UIImage(named: "ic_action_anchor"), for: .normal)
        view.addSubview(btn)
        
        let btn1 = getBtn(str: "btn1",btn: btn)
        view.addSubview(btn1)
        
        let btn2 = getBtn(str: "btn2",btn: btn1)
        btn2.contentEdgeInsets = UIEdgeInsets(top: 20, left: 40, bottom: 10, right: 10)
        view.addSubview(btn2)
        
        let btn3 = getBtn(str: "btn3",btn: btn2)
        btn3.imageEdgeInsets = UIEdgeInsets(top: 30, left: 70, bottom: 10, right: 10)
        view.addSubview(btn3)
        
        let btn4 = getBtn(str: "btn4",btn: btn3)
        btn4.titleEdgeInsets = UIEdgeInsets(top: -10, left: 0, bottom: 0, right: 0)
        view.addSubview(btn4)
    }
    
    func getBtn(str:String,btn:UIButton) -> UIButton {
        let btn = UIButton(frame: CGRect(x: 10, y: btn.frame.maxY + 20, width: 200, height: 50))
        btn.setTitle(str, for: .normal)
        btn.backgroundColor = UIColor.blue
        btn.setImage(UIImage(named: "ic_action_anchor"), for: .normal)
        return btn
    }
    
    @objc func clickMe() -> Void {
        view.backgroundColor = UIColor.init(displayP3Red: getColor(), green: getColor(), blue: getColor(), alpha: 1)
    }
    
    func getColor() -> CGFloat {
        return CGFloat(arc4random() % 255) / 255.0
    }
}
